package com.tz.controller;

import com.tz.entity.*;
import com.tz.entity.json.AndTempJson;
import com.tz.service.*;
import com.tz.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2018/1/5 0005.
 */
@Controller
@RequestMapping("/evallist")
public class CheckListcontroller {
    @Autowired
    ClazzService clazzService;
    @Autowired
    UserService userService;
    @Autowired
    RoleService roleService;
    @Autowired
    ResearchTemplateService researchTemplateService;
    @Autowired
    ResearchService researchService;
    @Autowired
    ResearchDetailService researchDetailService;
    private HashMap<Long, int[]> obj;
    private HashMap<Long, List<String>> ipMap;

    @ResponseBody
    @RequestMapping(value = "/clazztype", produces = "application/json")
    public List<Clazz> getClassType() {
        List<ClazzType> list = clazzService.getClazzType();
        List<Clazz> clazzes = new ArrayList<>();
        for (ClazzType e : list) {
            clazzes.addAll(clazzService.getAllCalzz(e));
        }
        System.out.println(">>>>>>>>>>>clazzes=" + clazzes);
        return clazzes;
    }

    @ResponseBody
    @RequestMapping(value = "/user", produces = "application/json")
    public List<User> getallusers() {

        return userService.findAllByRole(2L);
    }

    @ResponseBody
    @RequestMapping(value = "/template", produces = "application/json")
    public List<ResearchTemplate> getTemplates() {
        Integer status = 2;
        List<ResearchTemplate> templates = researchTemplateService.getAllResearchTemplate(status);
        System.out.println("<<<<<<<<templates=" + templates);
        return templates;
    }

    @ResponseBody
    @RequestMapping(value = "/checklist", produces = "application/json")
    public PageBean<Research> getAllResearch(@RequestParam("pageNow") Integer pageNow, @RequestParam("pageSize") Integer pageSize) {
        PageBean<Research> pageBean = researchService.findResearchsByPage(pageNow, pageSize, 0, null);
        System.out.println(">>>>>>>>>pageBean" + pageBean);

        return pageBean;
    }

    @ResponseBody
    @RequestMapping(value = "/add", produces = "application/json", method = RequestMethod.POST)
    public String getResearch(HttpServletRequest request, @RequestBody AndTempJson str) {
        System.out.println(str);


        Research research = new Research();
        Clazz clazz = clazzService.getClazz(str.getClazz());
        User user = userService.viewUserById(str.getUser());

        ResearchTemplate researchTemplate = researchTemplateService.getResearchTemplate(str.getTemp());
        researchTemplate.setStatus(2);
        researchTemplateService.alterTemplate(researchTemplate);
        research.setClazz(clazz);
        research.setUser(user);
        research.setState(0);
        research.setStartDate(new Date());
        researchService.addResearch(research, researchTemplate);
        //System.out.println(research.getResearchid());
        obj = (HashMap<Long, int[]>) request.getServletContext().getAttribute("countMap");
        ipMap = (HashMap<Long, List<String>>) request.getServletContext().getAttribute("ipMap");

        //加锁防止并发
        synchronized (obj) {
            obj.put(research.getResearchid(), new int[2]);
        }
        synchronized (ipMap) {

            ipMap.put(research.getResearchid(), new ArrayList<String>());
        }


        return "ok";
    }

    @ResponseBody
    @RequestMapping(value = "/end", produces = "application/json")
    public String endResearch(HttpServletRequest request, @RequestParam("Researchid") Long Researchid) {
        System.out.println(">>>>>>>>>id=" + Researchid);
        Research research = researchService.findById(Researchid);
        research.setState(1);
        research.setEndDate(new Date());
        research.setAver(researchDetailService.selectAVGByResearchID(Researchid));
        research.setTotalnum(researchDetailService.countDetailbyResearchId(Researchid));
        researchService.alterResearch(research);
        obj = (HashMap<Long, int[]>) request.getServletContext().getAttribute("countMap");
        //加锁防止并发
        synchronized (obj) {
            obj.remove(Researchid);
        }
        ipMap = (HashMap<Long, List<String>>) request.getServletContext().getAttribute("ipMap");
        synchronized (ipMap) {
            ipMap.remove(Researchid);
        }
        return "ok";
    }

    @ResponseBody
    @RequestMapping(value = "/count", produces = "application/json")
    public HashMap<Long, int[]> checkoutNumber(HttpServletRequest request) {

        HashMap<Long, int[]> res = (HashMap<Long, int[]>) request.getServletContext().getAttribute("countMap");

        return res;


    }

    @ResponseBody
    @RequestMapping(value = "/delete", produces = "application/json")
    public String deleteResearch(HttpServletRequest request, @RequestParam("Researchid") Long Researchid) {
        System.out.println(">>>>>>>>>id=" + Researchid);
        researchService.removeResearch(Researchid);
        obj = (HashMap<Long, int[]>) request.getServletContext().getAttribute("countMap");
        //加锁防止并发
        synchronized (obj) {
            obj.remove(Researchid);
        }
        ipMap= (HashMap<Long, List<String>>) request.getServletContext().getAttribute("ipMap");
        synchronized (ipMap) {
            ipMap.remove(Researchid);
        }

        return "ok";
    }
}
